package logger

import (
	"github.com/natefinch/lumberjack"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"os"
)

var Logger *zap.Logger

func InitLogger() {
	//设置lumberjack hook，将日志写入到本地文件中
	hook := lumberjack.Logger{
		Filename:  `./logs/lasted.log`, //文件名
		Compress:  true,                //是否开启历史日志压缩
		LocalTime: true,                //历史日志文件名是否使用本地时间而不是格林威治时间
	}

	fileWriter := zapcore.AddSync(&hook)

	consoleDebugging := zapcore.Lock(os.Stdout)
	consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())

	core := zapcore.NewTee(
		// 打印在控制台
		zapcore.NewCore(consoleEncoder, consoleDebugging, zap.DebugLevel),
		// 打印在文件中
		zapcore.NewCore(consoleEncoder, fileWriter, zap.DebugLevel),
	)

	Logger = zap.New(core)
}

func Sync() {
	_ = Logger.Sync()
}
